<script setup lang="ts">
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import Dialog from "@/view/Category/dialog.vue";
import SubDialog from "@/view/Category/sub-dialog.vue";
import useCategory from "@/view/Category/utils/hook.tsx";

const {
  dialog,
  subDialog,
  editSubCategory,
  categoryStore,
  addCategory,
  EditCategory,
  deleteCategory,
  addSubCategory
} = useCategory()
</script>

<template>
  <el-card class="card-category">
    <div class="card-category-list column">
      <div class="card-category-list-title">
        <el-button type="primary" @click="addCategory">添加分类</el-button>
      </div>
      <div class="card-category-list-table">
        <el-table
          border
          class="el-table-box"
          :data="categoryStore.categoryList"
          ref="multipleTableRef"
          style="width: 100%"
        >
          <el-table-column type="index" label="#" align="center"/>
          <el-table-column prop="_id" align="center" label="id"/>
          <el-table-column prop="name" align="center" label="名称" width="120"/>
          <el-table-column prop="nickname" align="center" label="昵称" width="120"/>
          <el-table-column align="center" label="图标" width="120">
            <template #default="{row}">
              <img style="width: 40px; height: 40px"
                   :src="row.icon" alt="">
            </template>
          </el-table-column>
          <el-table-column align="center" label="分类" width="200">
            <template #default="{row}">
              <el-tag v-for="item in row?.subCategoryId.slice(0, 6)" @click="editSubCategory(row._id, item)" :key="item._id" style="margin: 5px; cursor: pointer">
                {{ item.nickname }}
              </el-tag>
              <el-tag style="cursor: pointer" @click="addSubCategory(row._id)">+二级分类</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="控制" width="180">
            <template #default="{row}">
              <div class="operator-btn">
                <el-button type="primary" size="default" @click="EditCategory(row)" :icon="Edit as any" circle/>
                <el-popconfirm
                  width="200"
                  @confirm="deleteCategory()"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此部位吗?"
                >
                  <template #reference>
                    <el-button type="danger" size="default" :icon="Delete as any" circle/>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <Dialog ref="dialog"/>
    <SubDialog ref="subDialog"/>
  </el-card>
</template>

<style scoped lang="scss">
.card-category {
  padding: 0 50px;

  .card-category-list {

    .card-category-list-title {

      display: flex;
      justify-content: flex-end;
    }

    .card-category-list-table {
      margin-top: 20px;

      .el-table-box {
        height: calc(90vh - 140px);
      }
    }
  }
}
</style>
